<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/31
 * Time: 16:09
 */
namespace app\reserve\controller;

use think\Controller;
use think\Request;
use app\reserve\model\Onecard as CardModel;
use app\reserve\model\Uptorecord;

class Onecard extends Controller{

    /**
     * Notes:  一卡通列表
     * Create Date: 2018/10/31
     * Create by :lijie
     */
    public function oneCardList(Request $request)
    {
        $card        = new CardModel();
        $param      = $request->post();
        $resVali    = $this->validate($param,"Onecard.oneCardList");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        // 卡号筛选
        if(!empty(input('post.cardnumber'))){
            $where['o.cardnumber'] = array("LIKE",'%'.$param['cardnumber'].'%');
        }
        // 时间筛选
        if(!empty(input('post.start')) && !empty(input('post.end'))){
            $start = strtotime($param['start']." 00:00:00");
            $end   = strtotime($param['end']." 23:59:59");
            $where['o.ctime'] = array(array("EGT",$start),array("ELT",$end),'and');
        }
        $where['o.status'] = array("NEQ",9);
        $res['list'] = $card->selectOne($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $card->alias("o")->where($where)->count();
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }

    /**
     * Notes:  一卡通详情
     * Create Date: 2018/10/31
     * Create by :lijie
     */
    public function onecardInfo()
    {
        $u_id = input('post.u_id');
        if(empty($u_id)){
            return apiResponse(201,'用户id不能为空','');
        }
        //
        $card        = new CardModel();
        $where['u_id'] = $u_id;
        $where['status'] = array("NEQ",9);
        $res = $card->findOne($where);
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            $param['u_id']          = $u_id;
            $param['cardnumber']    = date("YmdHis",time()).mt_rand(100,999);
            $param['wallet']        = 0;
            $param['status']        = 0;
            $card->addOne($param);
            $res = $card->findOne($where);
            return apiResponse(200,"获取成功",$res);
        }
    }
    /*
    * Notes: 一卡通充值记录
    * Create Date: 2018/10/31
    * Create Time: 下午3:31
    * Create by :张福来
    */
    public function selectUptorecord(Request $request){
        $Uptorecord = new Uptorecord();
        $postData = $request->post();
        $check = $this->validate($postData,"Onecard.selectUptorecord");
        $pagenum = $postData['pagenum'];
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        if(!empty($postData["listNum"])){
            $listNum = $postData["listNum"];
        }else{
            $listNum = 10;
        }
        $w["u_id"] = $postData["u_id"];
        $w["status"] = array('NEQ',9);
        $result = $Uptorecord -> selectUptorecord($pagenum,$listNum,$w);
        if(empty($result["list"])){
            return apiResponse(200,"当前无数据",$result);
        }else{
            return apiResponse(200,"查询成功",$result);
        }
    }
}